package com.hmeter.hadoop.mapreduce.jobtracker.gui;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;

import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.gui.JLabeledTextField;

import com.hmeter.hadoop.mapreduce.jobtracker.JobTrackerSampler;

public class JobTrackerSamplerGui extends AbstractSamplerGui {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3571579901458947953L;
	
	private JLabeledTextField jobTrackerTextField = null;
	private JLabeledTextField jobIdTextField = null;
	private JLabeledTextField groupNameTextField = null;
	private JCheckBox jobTrackerMetricCheckBox = null;
	private JCheckBox jobCountersMetricCheckBox = null;
	private JCheckBox jobStatusMetricCheckBox = null;
	private JCheckBox taskCountersMetricCheckBox = null;
	private JCheckBox taskStatusMetricCheckBox = null;
	
	public JobTrackerSamplerGui() {
		super();
		init();
	}
	
	public void init() {
		setLayout(new BorderLayout());
        setBorder(makeBorder());
        add(makeTitlePanel(), BorderLayout.NORTH);
            
        jobTrackerTextField = new JLabeledTextField("Job tracker");
        jobIdTextField = new JLabeledTextField("Job id");
        groupNameTextField = new JLabeledTextField("Job counters group");
        jobTrackerMetricCheckBox = new JCheckBox("Get job tracker metric");
        jobCountersMetricCheckBox = new JCheckBox("Get job counters metric");
        jobStatusMetricCheckBox = new JCheckBox("Get job status metric");
        taskCountersMetricCheckBox = new JCheckBox("Get task counters metric");
        taskStatusMetricCheckBox = new JCheckBox("Get task status metric");
        
        ButtonGroup options = new ButtonGroup();
        options.add(jobTrackerMetricCheckBox);
        options.add(jobCountersMetricCheckBox);
        options.add(jobStatusMetricCheckBox);
        options.add(taskCountersMetricCheckBox);
        options.add(taskStatusMetricCheckBox);
        
        //main panel
        VerticalPanel panel = new VerticalPanel();
        panel.setBorder(BorderFactory.createEtchedBorder());
        
        //input data panel
        VerticalPanel inputPanel = new VerticalPanel();
        inputPanel.setBorder(BorderFactory.createTitledBorder("Input data"));
        
        inputPanel.add(jobTrackerTextField);
        inputPanel.add(jobIdTextField);
        inputPanel.add(groupNameTextField);
        
        //job tracker metric panel
        VerticalPanel jobTrackerMetricPanel = new VerticalPanel();
        jobTrackerMetricPanel.setBorder(BorderFactory.createTitledBorder("Job tracker metric"));
        jobTrackerMetricPanel.add(jobTrackerMetricCheckBox);
        
        //job metric panel
        VerticalPanel jobMetricPanel = new VerticalPanel();
        jobMetricPanel.setBorder(BorderFactory.createTitledBorder("Job metric"));
        jobMetricPanel.add(jobCountersMetricCheckBox);
        jobMetricPanel.add(jobStatusMetricCheckBox);
        
        //task metric panel
        VerticalPanel taskMetricPanel = new VerticalPanel();
        taskMetricPanel.setBorder(BorderFactory.createTitledBorder("Task metric"));
        taskMetricPanel.add(taskCountersMetricCheckBox);
        taskMetricPanel.add(taskStatusMetricCheckBox);
        
        panel.add(inputPanel);
        panel.add(jobTrackerMetricPanel);
        panel.add(jobMetricPanel);
        panel.add(taskMetricPanel);
        
        add(panel, BorderLayout.CENTER);
	}
	
	@Override
	public void clearGui()
	{
		super.clearGui();
		jobTrackerTextField.setText("");
		jobIdTextField.setText("");
		groupNameTextField.setText("");
		jobTrackerMetricCheckBox.setSelected(true);
		jobCountersMetricCheckBox.setSelected(false);
		taskCountersMetricCheckBox.setSelected(false);
	}
	
	@Override
	public String getLabelResource() {
		return getClass().getSimpleName();
	}
	
	@Override
	public String getStaticLabel() {
	    return "Job Tracker Sampler";
	}
	
	@Override
	public TestElement createTestElement() {
		JobTrackerSampler jobTrackerSampler = new JobTrackerSampler();
		modifyTestElement((TestElement) jobTrackerSampler);
		return (TestElement) jobTrackerSampler;
	}
	
	@Override
	public void modifyTestElement(TestElement element) {
		super.configureTestElement(element);
		if (element instanceof JobTrackerSampler)
		{
			JobTrackerSampler jobTrackerSampler = (JobTrackerSampler) element;
			jobTrackerSampler.setJobTracker(jobTrackerTextField.getText());
			jobTrackerSampler.setJobId(jobIdTextField.getText());
			jobTrackerSampler.setGroupName(groupNameTextField.getText());			
			jobTrackerSampler.setJobTrackerMetricBool(jobTrackerMetricCheckBox.isSelected());
			jobTrackerSampler.setJobCountersMetricBool(jobCountersMetricCheckBox.isSelected());
			jobTrackerSampler.setJobStatusMetricBool(jobStatusMetricCheckBox.isSelected());
			jobTrackerSampler.setTaskCountersMetricBool(taskCountersMetricCheckBox.isSelected());
			jobTrackerSampler.setTaskStatusMetricBool(taskStatusMetricCheckBox.isSelected());
		}
	}
	
	@Override
	public void configure(TestElement element)
	{
		super.configure(element);
		if (element instanceof JobTrackerSampler)
		{
			JobTrackerSampler jobTrackerSampler = (JobTrackerSampler) element;
			jobTrackerTextField.setText(jobTrackerSampler.getJobTracker());
			jobIdTextField.setText(jobTrackerSampler.getJobId());
			groupNameTextField.setText(jobTrackerSampler.getGroupName());
			jobTrackerMetricCheckBox.setSelected(jobTrackerSampler.isJobTrackerMetricBool());
			jobCountersMetricCheckBox.setSelected(jobTrackerSampler.isJobCountersMetricBool());
			jobStatusMetricCheckBox.setSelected(jobTrackerSampler.isJobStatusMetricBool());
			taskCountersMetricCheckBox.setSelected(jobTrackerSampler.isTaskCountersMetricBool());
			taskStatusMetricCheckBox.setSelected(jobTrackerSampler.isTaskStatusMetricBool());
		}
	}
}
